package com.bing.bingPro.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TZwIdentifyingDto;
import com.bing.bingPro.controller.dto.TZwMeteringVelocityDto;
import com.bing.bingPro.controller.vo.TBorderLatestStatsVo;
import com.bing.bingPro.entity.TBworderLstestStats;
import com.bing.bingPro.mapper.TBworderLstestStatsMapper;
import com.bing.bingPro.service.TBorderLatestStatsService;
import com.bing.bingPro.util.ProvinceUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
 * @author wangqilong
 * @description 针对表【t_bworder_lstest_stats】的数据库操作Service实现
 */
@Service
public class TBorderLatestStatsServiceImpl extends ServiceImpl<TBworderLstestStatsMapper, TBworderLstestStats>
        implements TBorderLatestStatsService {

    @Override
    public List<TBorderLatestStatsVo> tBorderLatestStatsList(TZwMeteringVelocityDto tZwMeteringVelocityDto) {
        String replace = tZwMeteringVelocityDto.getTime().replace("-", "");
        String province = tZwMeteringVelocityDto.getProvince();
        String orderType = ProvinceUtil.getOrderType(tZwMeteringVelocityDto.getOrderType());

        List<TBworderLstestStats> statsList = baseMapper.selectList(Wrappers.lambdaQuery(TBworderLstestStats.class)
                .eq(isNotEmpty(replace), TBworderLstestStats::getDataTime, replace)
                .eq(isNotEmpty(orderType), TBworderLstestStats::getOrderType, orderType)
                .eq(isNotEmpty(province), TBworderLstestStats::getProvinceName, province));

        if (!province.equals("全国") && isNotEmpty(province)) {
            List<TBworderLstestStats> cnList = baseMapper.selectList(Wrappers.lambdaQuery(TBworderLstestStats.class)
                    .eq(isNotEmpty(replace), TBworderLstestStats::getDataTime, replace)
                    .eq(isNotEmpty(orderType), TBworderLstestStats::getOrderType, orderType)
                    .eq(TBworderLstestStats::getProvinceName, "全国"));
            statsList.addAll(cnList);
        }

        return statsList.stream()
                .map(tBorderLatestStats -> mapToTBorderLatestStatsVo(tBorderLatestStats, tZwMeteringVelocityDto.getTime()))
                .collect(Collectors.toList());
    }

    private TBorderLatestStatsVo mapToTBorderLatestStatsVo(TBworderLstestStats tBorderLatestStats, String time) {
        TBorderLatestStatsVo vo = new TBorderLatestStatsVo();
        BeanUtils.copyProperties(tBorderLatestStats, vo);

        String orderType = tBorderLatestStats.getOrderType();
        switch (orderType) {
            case "cp":
                vo.setOrderType("投诉工单");
                break;
            case "install":
                vo.setOrderType("装机工单");
                break;
            default:
                break;
        }

        vo.setDataTime(time);
        return vo;
    }

    @Override
    public List<String> dateList(TZwIdentifyingDto tZwIdentifyingDto) {
        String time = tZwIdentifyingDto.getTime();
        switch (time) {
            case "nonStandard":
                return baseMapper.selectNonStandardDataList();
            case "notMeet":
                return baseMapper.selectNotMeetDateList();
            default:
                return baseMapper.selectTBorderLatestStatsDateList();
        }
    }

    @Override
    public List<String> getProvince(TZwIdentifyingDto tZwIdentifyingDto) {
        String province = tZwIdentifyingDto.getProvince();
        List<String> provinceList;

        switch (province) {
            case "nonStandard":
                provinceList = baseMapper.selectNonStandardProvince();
                break;
            case "notMeet":
                provinceList = baseMapper.selectNotMeetProvince();
                break;
            default:
                provinceList = baseMapper.selectTBworderLstestStatsProvince();
                break;
        }
        provinceList.add(0, "全部");
        return provinceList;
    }

    @Override
    public List<String> getOrderType(TZwIdentifyingDto tZwIdentifyingDto) {
        String orderType = tZwIdentifyingDto.getOrderType();
        List<String> orderTypeList;

        switch (orderType) {
            case "nonStandard":
                orderTypeList = baseMapper.selectNonStandardOrderType();
                break;
            case "notMeet":
                orderTypeList = baseMapper.selectNotMeetOrderType();
                break;
            default:
                orderTypeList = baseMapper.selectTBorderLatestStatsOrderType();
                break;
        }
        orderTypeList.add(0, "全部");
        return orderTypeList;
    }
}




